package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.RecordStudentLessonApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.domain.entity.RecordStudentLessonEntity;
import com.example.wisdomclassroomapi.facade.assembler.RecordStudentLessonFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.RecordStudentLessonRequestDto;
import com.example.wisdomclassroomapi.facade.dto.RecordStudentLessonResponseDto;
import com.example.wisdomclassroomapi.infrastructure.mapper.RecordStudentLessonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/recordStudentLesson")
@Slf4j
public class RecordStudentLessonController extends BaseController {
@Resource
private RecordStudentLessonApplicationService recordStudentLessonApplicationService;

@Resource
private RecordStudentLessonMapper recordStudentLessonMapper;

@Resource
private RecordStudentLessonFacadeAssembler recordStudentLessonFacadeAssembler;

/**
* 分页查询
*/
@PostMapping("/page")
public PageDto<List<RecordStudentLessonResponseDto>> getRecordStudentLessonByPage(@RequestBody @Valid RecordStudentLessonRequestDto request) {
        IPage<RecordStudentLessonEntity> page = new Page<RecordStudentLessonEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
       RecordStudentLessonEntity entity= recordStudentLessonFacadeAssembler.assembleRecordStudentLessonEntity(request);
        page =  recordStudentLessonApplicationService.page(entity, page);
        List<RecordStudentLessonResponseDto> assembleData = new ArrayList<>();
        if (page != null&& page.getRecords()!=null&&page.getRecords().size() > 0) {
        for (RecordStudentLessonEntity item : page.getRecords()) {
           assembleData.add(recordStudentLessonFacadeAssembler.assembleRecordStudentLessonResponseDto(item));
        }
        }
        PageDto<List<RecordStudentLessonResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setData(assembleData);
        return result;
}


/**
* 查询
*/
@PostMapping("/list")
public List<RecordStudentLessonResponseDto> getRecordStudentLessons(@RequestBody @Valid RecordStudentLessonRequestDto request) {
       RecordStudentLessonEntity entity= recordStudentLessonFacadeAssembler.assembleRecordStudentLessonEntity(request);
        List<RecordStudentLessonEntity> list= recordStudentLessonApplicationService.list(entity);
        List<RecordStudentLessonResponseDto> assembleData = new ArrayList<>();
        if(list!=null&&list.size()>0){
           for (RecordStudentLessonEntity item : list) {
              assembleData.add(recordStudentLessonFacadeAssembler.assembleRecordStudentLessonResponseDto(item));
           }
        }
        return assembleData;
}
/**
* 获取某一实体
*/
@PostMapping("/getRecordStudentLesson")
public RecordStudentLessonResponseDto getRecordStudentLessonDetails(String id) {
       RecordStudentLessonEntity entity = recordStudentLessonApplicationService.findById(id);
       RecordStudentLessonResponseDto responseDto=null;
        if(entity!=null){
            responseDto= recordStudentLessonFacadeAssembler.assembleRecordStudentLessonResponseDto(entity);
        }
        return responseDto;
}
/**
* 新增数据
*/
@PostMapping("/add")
public void saveRecordStudentLesson(@RequestBody RecordStudentLessonRequestDto request) {
       RecordStudentLessonEntity entity= recordStudentLessonFacadeAssembler.assembleRecordStudentLessonEntity(request);
       recordStudentLessonApplicationService.add(entity);
}
/**
* 修改数据
*/
@PostMapping("/update")
public void modifyRecordStudentLesson(@RequestBody RecordStudentLessonRequestDto request) {
       RecordStudentLessonEntity entity= recordStudentLessonFacadeAssembler.assembleRecordStudentLessonEntity(request);
       recordStudentLessonApplicationService.updateById(entity);
}

/**
* 删除数据
*/
@PostMapping("/delete")
public void removeRecordStudentLesson(@RequestBody @Valid  String id) {
       recordStudentLessonApplicationService.deleteById(id);
}

@PostMapping("/lately")
public RecordStudentLessonResponseDto getLately() {
    String userId = this.getLoginUserId();
    QueryWrapper<RecordStudentLessonEntity> wrapper = new QueryWrapper<RecordStudentLessonEntity>()
            .eq("student_id", userId)
            .orderByDesc("update_time")
            .last("limit 1");
    return recordStudentLessonFacadeAssembler.assembleRecordStudentLessonResponseDto(recordStudentLessonMapper.selectOne(wrapper));
}
}